package com.java123.web.t2.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class TestParamServlet extends HttpServlet {

	private static final long serialVersionUID = -4933000781451987190L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//---- 请求处理 ----
		//1、获取参数
		request.getParameter("");//单个参数
		request.getParameterMap();//所有的参数map
		request.getParameterNames();//所有参数的键
		request.getParameterValues("checkbox");//复选框的值
		String name = request.getParameter("name");
		System.out.println(name);
		System.out.println(JSONObject.toJSONString(request.getParameterNames()));;
		
		//2、获取headers
		request.getHeaderNames();//所有的header 键
		String userAgent = request.getHeader("user-agent");
		System.out.println(userAgent);
		System.out.println(JSONObject.toJSONString(request.getHeaderNames()));
		//key=value
		//3、获取客户端相关信息
		String ip = request.getRemoteAddr();
		System.out.println(ip);
		
		//4、请求转发  可以是内部转发，请求数据保留
//		request.setAttribute("userId", "userId");
//		request.getRequestDispatcher("/hello").forward(request, response);
		//---- 请求处理 ----
		
		//---- 响应处理 ----
		//常用响应
		//1、响应数据，writer
//		response.getWriter().write("success");
		//2、重定向 
		response.sendRedirect("http://www.baidu.com");
		//---- 响应处理 ----
		
		
		//下次讲到
		//session相关
		//servlet context(上下文)
	}
}
